package com.mediacloud.app.newsmodule.fragment.baoliao.adapter

import android.content.Context
import android.graphics.drawable.Drawable
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.mediacloud.app.assembly.util.Utility
import com.mediacloud.app.assembly.views.CornerBlurView
import com.mediacloud.app.nav2.utils.DateUtils
import com.mediacloud.app.newsmodule.R
import com.mediacloud.app.newsmodule.activity.microlive.BaseRecyclerAdapterX
import com.mediacloud.app.newsmodule.model.baoliao.list.list.BaoNiaoMedia
import com.mediacloud.app.reslib.enums.AppFactoryGlobalConfig
import org.json.JSONException
import org.json.JSONObject

/**
 * 爆料列表中的图片视频宫格
 */
class BaoLiaoListGridAdapter(context: Context) : BaseRecyclerAdapterX<BaoNiaoMedia>(context) {

    /*  companion object {
          val sparryArray = SparseArray<BaoLiaoPictureInfo>()
      }*/

    constructor(context: Context, spanCount: Int) : this(context) {
        this.spancount = spanCount
    }

    var spancount = -1
    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): androidx.recyclerview.widget.RecyclerView.ViewHolder {
        return BaoLiaoGridItemHolder(
            LayoutInflater.from(context)
                .inflate(R.layout.baoniao_list_inner_recycerview_item, parent, false)
        )
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        super.onBindViewHolder(holder, position)
        if (holder is BaoLiaoGridItemHolder) {
            holder.setItemData(position, getItem(position))
        }
    }


    inner class BaoLiaoGridItemHolder(itemView: View) :
        androidx.recyclerview.widget.RecyclerView.ViewHolder(itemView)/*, ViewTreeObserver.OnPreDrawListener */ {
        private val defaultLoading: Drawable = AppFactoryGlobalConfig.getDefaultImageLoadDrawable()
        var baoniaotupian: CornerBlurView = itemView.findViewById(R.id.baoniaotupian)
        var videoTag: TextView = itemView.findViewById(R.id.videoTag);

        //    var baoniaovideoicon: ImageView = itemView.findViewById(R.id.baoniaovideoicon)
        //   var baoniaovideoiconlb: ImageView = itemView.findViewById(R.id.baoniaovideoiconlb)
        var round = itemView.resources.getDimension(R.dimen.defaultloading_roundradius)

        /* init {
             val baoLiaoPictureInfo = sparryArray.get(spancount)
             if (baoLiaoPictureInfo != null && baoLiaoPictureInfo.canUse()) {
                 baoniaotupian.layoutParams.width = baoLiaoPictureInfo.width
                 baoniaotupian.layoutParams.height = baoLiaoPictureInfo.height
                 baoniaotupian.layoutParams
                 baoniaotupian.minimumWidth = baoLiaoPictureInfo.width
                 baoniaotupian.minimumHeight = baoLiaoPictureInfo.height
                 baoniaotupian.requestLayout()
             }
             baoniaotupian.viewTreeObserver.addOnPreDrawListener(this)
         }*/

        fun setItemData(position: Int, baoNiaoMedia: BaoNiaoMedia) {
            val logoHeight: Float = if (spancount == 1) {
                9f * Utility.getScreenSize(itemView.context)[0] / 16f
            } else if (spancount == 2) {
                val singleWidth =
                    (Utility.getScreenSize(itemView.context)[0] - context.resources.getDimension(R.dimen.dimen26)) / 2
                singleWidth * 108 / 165
            } else if (spancount == 3) {
                val singleWidth =
                    (Utility.getScreenSize(itemView.context)[0] - context.resources.getDimension(R.dimen.dimen26)) / 3
                singleWidth * 84 / 109
            } else {
                9f * Utility.getScreenSize(itemView.context)[0] / 16f
            }
            val lp = baoniaotupian.layoutParams
            lp.height = logoHeight.toInt()
            baoniaotupian.layoutParams = lp
            val visible = if (baoNiaoMedia.isVideo) View.VISIBLE else View.GONE
            videoTag.visibility = visible
//            if (spancount == 1 && visible == View.VISIBLE) {
//                baoniaovideoiconlb.visibility = visible
//                baoniaovideoicon.visibility = View.GONE
//            } else {
//                baoniaovideoiconlb.visibility = View.GONE
//                baoniaovideoicon.visibility = visible
//            }

            if (baoNiaoMedia.videoObj != null) {
                val formats = baoNiaoMedia.videoObj.optJSONArray("formats")
                if (formats != null && formats.length() > 0) {
                    try {
                        val duration = (formats[0] as JSONObject).optString("duration")
                        val image = (formats[0] as JSONObject).optString("image")
                        if (!TextUtils.isEmpty(duration)) {
                            videoTag.text =
                                DateUtils.millsecondToMinuteSecond(duration.toInt() * 1000)
                        }
                        if (baoNiaoMedia.isVideo && !TextUtils.isEmpty(image)) {
                            baoNiaoMedia.img = image
                        }
                    } catch (e: JSONException) {
                        e.printStackTrace()
                    }
                }
            }
            val url: String? = baoNiaoMedia.img

            if (spancount == 1) {
                baoniaotupian.load(url, true)

            } else if (spancount == 3) {
                if (data.size <= 3) {
                    when (position) {
                        0 -> baoniaotupian.load(url, round, 0f, round, 0f)
                        1 -> baoniaotupian.load(url, 0f, 0f, 0f, 0f)
                        2 -> baoniaotupian.load(url, 0f, round, 0f, round)

                    }
                } else if (data.size <= 6) {
                    when (position) {
                        0 -> baoniaotupian.load(url, round, 0f, 0f, 0f)
                        1 -> baoniaotupian.load(url, 0f, 0f, 0f, 0f)
                        2 -> baoniaotupian.load(url, 0f, round, 0f, 0f)
                        3 -> baoniaotupian.load(url, 0f, 0f, round, 0f)
                        4 -> baoniaotupian.load(url, 0f, 0f, 0f, 0f)
                        5 -> baoniaotupian.load(url, 0f, 0f, 0f, round)
                    }
                } else {
                    when (position) {
                        0 -> baoniaotupian.load(url, round, 0f, 0f, 0f)
                        1 -> baoniaotupian.load(url, 0f, 0f, 0f, 0f)
                        2 -> baoniaotupian.load(url, 0f, round, 0f, 0f)
                        6 -> baoniaotupian.load(url, 0f, 0f, round, 0f)
                        7 -> baoniaotupian.load(url, 0f, 0f, 0f, 0f)
                        8 -> baoniaotupian.load(url, 0f, 0f, 0f, round)
                        else -> baoniaotupian.load(url, 0f, 0f, 0f, 0f)
                    }
                }

            } else if (spancount == 2) {
                if (data.size <= 2) {
                    when (position) {
                        0 -> baoniaotupian.load(url, round, 0f, round, 0f)
                        1 -> baoniaotupian.load(url, 0f, round, 0f, round)
                    }
                } else if (data.size <= 4) {
                    when (position) {
                        0 -> baoniaotupian.load(url, round, 0f, 0f, 0f)
                        1 -> baoniaotupian.load(url, 0f, round, 0f, 0f)
                        2 -> baoniaotupian.load(url, 0f, 0f, round, 0f)
                        3 -> baoniaotupian.load(url, 0f, 0f, 0f, round)
                    }
                } else {
                    baoniaotupian.load(url, true)
                }
            } else {
                baoniaotupian.load(url, true)
            }

        }

        /*    override fun onPreDraw(): Boolean {
                if (itemView.parent is RecyclerView) {
                    val parentWidth = (itemView.parent as RecyclerView).measuredWidth
                    val singleWidth = (parentWidth - itemView.resources.getDimensionPixelSize(R.dimen.dime_five) * (spancount - 1)) / spancount
                    val height = (singleWidth / (16f / 9.0f)).toInt()
                    baoniaotupian.layoutParams.width = singleWidth
                    baoniaotupian.layoutParams.height = height
                    baoniaotupian.layoutParams
                    baoniaotupian.minimumWidth = singleWidth
                    baoniaotupian.minimumHeight = height
                    baoniaotupian.requestLayout()
                    sparryArray.put(spancount, BaoLiaoPictureInfo(singleWidth, height))
                }

                baoniaovideoicon.viewTreeObserver.removeOnPreDrawListener(this)
                return true
            }*/
    }

}